package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.StationCellReflectionDto;
import com.carrot.common.core.domain.entity.SysStationCellReflection;
import com.carrot.common.core.domain.entity.SysStationCellType;
import com.carrot.common.core.domain.vo.StationCellReflectionVo;
import com.carrot.common.enums.CellReflectionTypeEnum;
import com.carrot.common.enums.CellTypeEnum;
import com.carrot.common.exception.ServiceException;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.system.service.ISysStationCellReflectionService;
import com.carrot.system.service.ISysStationCellTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/station-cell-reflection")
public class SysStationCellReflectionController extends BaseController {

    @Autowired
    private ISysStationCellTypeService cellTypeService;

    @Autowired
    private ISysStationCellReflectionService cellReflectionService;

    /**
     * 获取场站单体映射
     */
    @GetMapping("list")
    public AjaxResult list(@RequestParam("cellTypeId") Long cellTypeId) {

        SysStationCellType cellType = cellTypeService.query()
                .eq("id", cellTypeId).one();
        if (null == cellType) {
            throw new ServiceException("不存在该单体类型");
        }

        List<SysStationCellReflection> list = cellReflectionService
                .query()
                .eq("cell_type_id", cellTypeId)
                .eq("station_id", cellType.getStationId())
                .orderByAsc("create_time")
                .list();

        List<StationCellReflectionVo> voList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(list)) {
            for (SysStationCellReflection cellReflection : list) {

                StationCellReflectionVo cellReflectionVo = WrapperUtil.transform(cellReflection, StationCellReflectionVo.class);

                cellReflectionVo.setCellTypeName(CellTypeEnum.getCellTypeName(cellReflectionVo.getCellType()));
                cellReflectionVo.setReflectionTypeName(CellReflectionTypeEnum.getCellReflectionTypeName(cellReflectionVo.getReflectionType()));
                voList.add(cellReflectionVo);
            }
        }

        return AjaxResult.success(voList);
    }

    /**
     * 新增
     */
    @PostMapping("add")
    public AjaxResult add(@RequestBody StationCellReflectionDto dto) {
        if (null == dto.getCellTypeId() || CollUtil.isEmpty(dto.getList())) {
            throw new ServiceException("请求参数校验失败");
        }
        cellReflectionService.updateCellReflection(dto);
        return AjaxResult.success();
    }

    /**
     * 更新
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody StationCellReflectionDto dto) {

        if (null == dto.getCellTypeId() || CollUtil.isEmpty(dto.getList())) {
            throw new ServiceException("请求参数校验失败");
        }
        cellReflectionService.updateCellReflection(dto);
        return AjaxResult.success();
    }

    /**
     * 删除
     */
    @GetMapping("del")
    public AjaxResult del(@RequestParam("cellTypeId") Long cellTypeId) {

        cellReflectionService.delCellReflection(cellTypeId);

        return AjaxResult.success();
    }


}
